# -*- coding: utf-8 -*-
"""
Created on Fri Dec 13 15:13:42 2024

@author: 35139
"""

import networkx as nx
import numpy as np
N = 1000
# 设置随机数种子
np.random.seed(1)

def write2File(G,filename,n):
    """
    将图G输出为文本数据,格式为
    u,v,weight
    """
    with open(filename, 'w') as f:
        f.write(f"{n}\n") #首先输出矩阵的大小,从0到n-1个节点
        for u, v, data in G.edges(data=True):
            f.write(f"{u},{v},1\n")
        f.write('-'*10)
        f.write('\n')
        flag = (int)(nx.is_directed_acyclic_graph(G))
        # 0表示 False ,1 表示 True
        f.write(f"{flag}")
        # print(flag) 数据是比较均衡的
        pass
        
            

def createG(n,p,seed):
    """
    创建图G,必须是无向图
    """
    G = nx.erdos_renyi_graph(n, p,seed = seed,directed = True)
    # 为每条边分配随机整数权重
    return G



if __name__ == "__main__":
    for i in range(N):
        n = np.random.randint(3,30)
        p = np.random.rand(1)/10 + 0.01
        seed = np.random.randint(1, 1000000000)

        G = createG(n, p, seed)
        filename = "DGAData/graph_" + str(i) + ".txt"
        write2File(G, filename,n)

        
        